/*     */ package com.zimbra.cs.util;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.auth.ZAuthToken;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.AdminConstants;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.Element.XMLElement;
/*     */ import com.zimbra.common.soap.SoapHttpTransport;
/*     */ import com.zimbra.common.util.CliUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.httpclient.URLUtil;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.CommandLineParser;
/*     */ import org.apache.commons.cli.GnuParser;
/*     */ import org.apache.commons.cli.Options;
/*     */ import org.apache.commons.cli.ParseException;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class GalSyncAccountUtil
/*     */ {
/*     */   private static final int CREATE_ACCOUNT = 10;
/*     */   private static final int ADD_DATASOURCE = 11;
/*     */   private static final int DELETE_ACCOUNT = 12;
/*     */   private static final int TRICKLE_SYNC = 13;
/*     */   private static final int FULL_SYNC = 14;
/*     */   private static final int FORCE_SYNC = 15;
/*     */   private static final String CREATE_ACCOUNT_COMMAND = "createaccount";
/*     */   private static final String ADD_DATASOURCE_COMMAND = "adddatasource";
/*     */   private static final String DELETE_ACCOUNT_COMMAND = "deleteaccount";
/*     */   private static final String TRICKLE_SYNC_COMMAND = "tricklesync";
/*     */   private static final String FULL_SYNC_COMMAND = "fullsync";
/*     */   private static final String FORCE_SYNC_COMMAND = "forcesync";
/*     */   private static Map<String, Integer> mCommands;
/*     */   private String mUsername;
/*     */   private String mPassword;
/*     */   private String mAdminURL;
/*     */   private ZAuthToken mAuth;
/*     */   private SoapHttpTransport mTransport;
/*     */   private String mAccountId;
/*     */   private String mAccountName;
/*     */   private String mDataSourceId;
/*     */   private String mDataSourceName;
/*     */   private boolean mFullSync;
/*     */   private boolean mForceSync;
/*     */   private String mServer;
/*     */   
/*     */   private static void usage()
/*     */   {
/*  62 */     System.out.println("zmgsautil: {command}");
/*  63 */     System.out.println("\tcreateAccount -a {account-name} -n {datasource-name} --domain {domain-name} -t zimbra|ldap -s {server} [-f {folder-name}] [-p {polling-interval}]");
/*  64 */     System.out.println("\taddDataSource -a {account-name} -n {datasource-name} --domain {domain-name} -t zimbra|ldap [-f {folder-name}] [-p {polling-interval}]");
/*  65 */     System.out.println("\tdeleteAccount [-a {account-name} | -i {account-id}]");
/*  66 */     System.out.println("\ttrickleSync [-a {account-name} | -i {account-id}] [-d {datasource-id}] [-n {datasource-name}]");
/*  67 */     System.out.println("\tfullSync [-a {account-name} | -i {account-id}] [-d {datasource-id}] [-n {datasource-name}]");
/*  68 */     System.out.println("\tforceSync [-a {account-name} | -i {account-id}] [-d {datasource-id}] [-n {datasource-name}]");
/*  69 */     System.exit(1);
/*     */   }
/*     */   
/*     */   private static void addCommand(String cmd, int cmdId) {
/*  73 */     mCommands.put(cmd, Integer.valueOf(cmdId));
/*     */   }
/*     */   
/*     */   private static int lookupCmd(String cmd) {
/*  77 */     Integer i = (Integer)mCommands.get(cmd.toLowerCase());
/*  78 */     if (i == null) {
/*  79 */       usage();
/*     */     }
/*  81 */     return i.intValue();
/*     */   }
/*     */   
/*     */   private static void setup() {
/*  85 */     mCommands = new HashMap();
/*  86 */     addCommand("createaccount", 10);
/*  87 */     addCommand("adddatasource", 11);
/*  88 */     addCommand("deleteaccount", 12);
/*  89 */     addCommand("tricklesync", 13);
/*  90 */     addCommand("fullsync", 14);
/*  91 */     addCommand("forcesync", 15);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private GalSyncAccountUtil()
/*     */   {
/* 101 */     String server = LC.zimbra_zmprov_default_soap_server.value();
/* 102 */     this.mAdminURL = URLUtil.getAdminURL(server);
/* 103 */     this.mUsername = LC.zimbra_ldap_user.value();
/* 104 */     this.mPassword = LC.zimbra_ldap_password.value();
/*     */   }
/*     */   
/*     */   private void checkArgs() throws ServiceException {
/* 108 */     if (this.mAccountName != null) {
/* 109 */       Account acct = Provisioning.getInstance().getAccountByName(this.mAccountName);
/* 110 */       if (acct == null)
/* 111 */         throw AccountServiceException.NO_SUCH_ACCOUNT(this.mAccountName);
/* 112 */       this.mAccountId = acct.getId();
/*     */     }
/* 114 */     if ((this.mAccountId == null) || ((this.mDataSourceId == null) && (this.mDataSourceName == null))) {
/* 115 */       usage();
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private void syncGalAccount()
/*     */     throws ServiceException, IOException
/*     */   {
/* 127 */     checkArgs();
/* 128 */     this.mTransport = null;
/*     */     try {
/* 130 */       this.mTransport = new SoapHttpTransport(this.mAdminURL);
/* 131 */       auth();
/* 132 */       this.mTransport.setAuthToken(this.mAuth);
/* 133 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.SYNC_GAL_ACCOUNT_REQUEST);
/* 134 */       Element acct = req.addElement("account");
/* 135 */       acct.addAttribute("id", this.mAccountId);
/* 136 */       Element ds = acct.addElement("datasource");
/* 137 */       if (this.mDataSourceId != null) {
/* 138 */         ds.addAttribute("by", "id").setText(this.mDataSourceId);
/*     */       } else
/* 140 */         ds.addAttribute("by", "name").setText(this.mDataSourceName);
/* 141 */       if (this.mFullSync)
/* 142 */         ds.addAttribute("fullSync", "TRUE");
/* 143 */       if (this.mForceSync) {
/* 144 */         ds.addAttribute("reset", "TRUE");
/*     */       }
/* 146 */       this.mTransport.invoke(req);
/*     */     } finally {
/* 148 */       if (this.mTransport != null)
/* 149 */         this.mTransport.shutdown();
/*     */     }
/*     */   }
/*     */   
/* 153 */   private Element createGalSyncAccount(String accountName, String dsName, String domain, String type, String folder, String pollingInterval, String mailHost) throws ServiceException, IOException { this.mTransport = null;
/*     */     try {
/* 155 */       this.mTransport = new SoapHttpTransport(this.mAdminURL);
/* 156 */       auth();
/* 157 */       this.mTransport.setAuthToken(this.mAuth);
/* 158 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.CREATE_GAL_SYNC_ACCOUNT_REQUEST);
/* 159 */       req.addAttribute("name", dsName);
/* 160 */       req.addAttribute("domain", domain);
/* 161 */       req.addAttribute("type", type);
/* 162 */       if (folder != null)
/* 163 */         req.addAttribute("folder", folder);
/* 164 */       req.addAttribute("server", mailHost);
/* 165 */       Element acct = req.addElement("account");
/* 166 */       acct.addAttribute("by", Key.AccountBy.name.name());
/* 167 */       acct.setText(accountName);
/* 168 */       if (pollingInterval != null) {
/* 169 */         req.addElement("a").addAttribute("n", "zimbraDataSourcePollingInterval").setText(pollingInterval);
/*     */       }
/* 171 */       return this.mTransport.invokeWithoutSession(req);
/*     */     } finally {
/* 173 */       if (this.mTransport != null)
/* 174 */         this.mTransport.shutdown();
/*     */     }
/*     */   }
/*     */   
/* 178 */   private Element addGalSyncDataSource(String accountName, String dsName, String domain, String type, String folder, String pollingInterval) throws ServiceException, IOException { this.mTransport = null;
/*     */     try {
/* 180 */       this.mTransport = new SoapHttpTransport(this.mAdminURL);
/* 181 */       auth();
/* 182 */       this.mTransport.setAuthToken(this.mAuth);
/* 183 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.ADD_GAL_SYNC_DATASOURCE_REQUEST);
/* 184 */       req.addAttribute("name", dsName);
/* 185 */       req.addAttribute("domain", domain);
/* 186 */       req.addAttribute("type", type);
/* 187 */       if (folder != null)
/* 188 */         req.addAttribute("folder", folder);
/* 189 */       Element acct = req.addElement("account");
/* 190 */       acct.addAttribute("by", Key.AccountBy.name.name());
/* 191 */       acct.setText(accountName);
/* 192 */       if (pollingInterval != null) {
/* 193 */         req.addElement("a").addAttribute("n", "zimbraDataSourcePollingInterval").setText(pollingInterval);
/*     */       }
/* 195 */       return this.mTransport.invokeWithoutSession(req);
/*     */     } finally {
/* 197 */       if (this.mTransport != null)
/* 198 */         this.mTransport.shutdown();
/*     */     }
/*     */   }
/*     */   
/* 202 */   private Element deleteGalSyncAccount(String name, String id) throws ServiceException, IOException { this.mTransport = null;
/*     */     try {
/* 204 */       this.mTransport = new SoapHttpTransport(this.mAdminURL);
/* 205 */       auth();
/* 206 */       this.mTransport.setAuthToken(this.mAuth);
/* 207 */       Element.XMLElement req = new Element.XMLElement(AdminConstants.DELETE_GAL_SYNC_ACCOUNT_REQUEST);
/* 208 */       Element acct = req.addElement("account");
/*     */       String account;
/*     */       Key.AccountBy by;
/* 211 */       String account; if (name == null) {
/* 212 */         Key.AccountBy by = Key.AccountBy.id;
/* 213 */         account = id;
/*     */       } else {
/* 215 */         by = Key.AccountBy.name;
/* 216 */         account = name;
/*     */       }
/* 218 */       acct.addAttribute("by", by.name());
/* 219 */       acct.setText(account);
/* 220 */       return this.mTransport.invokeWithoutSession(req);
/*     */     } finally {
/* 222 */       if (this.mTransport != null)
/* 223 */         this.mTransport.shutdown();
/*     */     }
/*     */   }
/*     */   
/* 227 */   private void setAccountId(String aid) { this.mAccountId = aid; }
/*     */   
/*     */   private void setAccountName(String name) {
/* 230 */     this.mAccountName = name;
/*     */   }
/*     */   
/* 233 */   private void setDataSourceId(String did) { this.mDataSourceId = did; }
/*     */   
/*     */   private void setDataSourceName(String name) {
/* 236 */     this.mDataSourceName = name;
/*     */   }
/*     */   
/* 239 */   private void setFullSync() { this.mFullSync = true; }
/*     */   
/*     */ 
/* 242 */   private void setForceSync() { this.mForceSync = true; }
/*     */   
/*     */   private void auth() throws ServiceException, IOException {
/* 245 */     Element.XMLElement req = new Element.XMLElement(AdminConstants.AUTH_REQUEST);
/* 246 */     req.addElement("name").setText(this.mUsername);
/* 247 */     req.addElement("password").setText(this.mPassword);
/* 248 */     Element resp = this.mTransport.invoke(req);
/* 249 */     this.mAuth = new ZAuthToken(resp.getElement("authToken"), true);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception {
/* 253 */     if (args.length < 1)
/* 254 */       usage();
/* 255 */     CliUtil.toolSetup();
/* 256 */     CommandLineParser parser = new GnuParser();
/* 257 */     Options options = new Options();
/* 258 */     options.addOption("a", "account", true, "gal sync account name");
/* 259 */     options.addOption("i", "id", true, "gal sync account id");
/* 260 */     options.addOption("n", "name", true, "datasource name");
/* 261 */     options.addOption("d", "did", true, "datasource id");
/* 262 */     options.addOption("x", "domain", true, "for domain gal sync account");
/* 263 */     options.addOption("f", "folder", true, "folder id");
/* 264 */     options.addOption("p", "polling", true, "polling interval");
/* 265 */     options.addOption("t", "type", true, "gal type");
/* 266 */     options.addOption("s", "server", true, "mailhost");
/* 267 */     options.addOption("h", "help", true, "help");
/* 268 */     CommandLine cl = null;
/* 269 */     boolean err = false;
/*     */     try {
/* 271 */       cl = parser.parse(options, args, false);
/*     */     } catch (ParseException pe) {
/* 273 */       System.out.println("error: " + pe.getMessage());
/* 274 */       err = true;
/*     */     }
/*     */     
/* 277 */     GalSyncAccountUtil cli = new GalSyncAccountUtil();
/* 278 */     if ((err) || (cl.hasOption('h'))) {
/* 279 */       usage();
/*     */     }
/* 281 */     if (cl.hasOption('i'))
/* 282 */       cli.setAccountId(cl.getOptionValue('i'));
/* 283 */     if (cl.hasOption('a'))
/* 284 */       cli.setAccountName(cl.getOptionValue('a'));
/* 285 */     if (cl.hasOption('n'))
/* 286 */       cli.setDataSourceName(cl.getOptionValue('n'));
/* 287 */     if (cl.hasOption('d'))
/* 288 */       cli.setDataSourceId(cl.getOptionValue('d'));
/* 289 */     setup();
/* 290 */     int cmd = lookupCmd(args[0]);
/*     */     try { String acctName;
/* 292 */       String dsName; String domain; String type; String folderName; String pollingInterval; switch (cmd) {
/*     */       case 13: 
/* 294 */         cli.syncGalAccount();
/* 295 */         break;
/*     */       case 14: 
/* 297 */         cli.setFullSync();
/* 298 */         cli.syncGalAccount();
/* 299 */         break;
/*     */       case 15: 
/* 301 */         cli.setForceSync();
/* 302 */         cli.syncGalAccount();
/* 303 */         break;
/*     */       case 10: 
/* 305 */         acctName = cl.getOptionValue('a');
/* 306 */         dsName = cl.getOptionValue('n');
/* 307 */         domain = cl.getOptionValue('x');
/* 308 */         type = cl.getOptionValue('t');
/* 309 */         folderName = cl.getOptionValue('f');
/* 310 */         pollingInterval = cl.getOptionValue('p');
/* 311 */         String mailHost = cl.getOptionValue('s');
/* 312 */         if ((acctName == null) || (mailHost == null) || (dsName == null) || (type == null) || ((type.compareTo("zimbra") != 0) && (type.compareTo("ldap") != 0)))
/* 313 */           usage();
/* 314 */         for (Element account : cli.createGalSyncAccount(acctName, dsName, domain, type, folderName, pollingInterval, mailHost).listElements("account"))
/* 315 */           System.out.println(account.getAttribute("name") + "\t" + account.getAttribute("id"));
/* 316 */         break;
/*     */       case 11: 
/* 318 */         acctName = cl.getOptionValue('a');
/* 319 */         dsName = cl.getOptionValue('n');
/* 320 */         domain = cl.getOptionValue('x');
/* 321 */         type = cl.getOptionValue('t');
/* 322 */         folderName = cl.getOptionValue('f');
/* 323 */         pollingInterval = cl.getOptionValue('p');
/* 324 */         if ((acctName == null) || (dsName == null) || (type == null) || ((type.compareTo("zimbra") != 0) && (type.compareTo("ldap") != 0)))
/* 325 */           usage();
/* 326 */         for (Element account : cli.addGalSyncDataSource(acctName, dsName, domain, type, folderName, pollingInterval).listElements("account"))
/* 327 */           System.out.println(account.getAttribute("name") + "\t" + account.getAttribute("id"));
/* 328 */         break;
/*     */       case 12: 
/* 330 */         String name = cl.getOptionValue('a');
/* 331 */         String id = cl.getOptionValue('i');
/* 332 */         if ((name == null) && (id == null))
/* 333 */           usage();
/* 334 */         cli.deleteGalSyncAccount(name, id);
/* 335 */         break;
/*     */       default: 
/* 337 */         usage();
/*     */       }
/*     */     } catch (ServiceException se) {
/* 340 */       System.out.println("Error: " + se.getMessage());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/GalSyncAccountUtil.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */